package com.weme.mine.fragment

import android.graphics.drawable.Drawable
import android.os.Bundle
import android.text.TextUtils
import androidx.lifecycle.ViewModelProvider
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.Target
import com.weme.common.base.BaseFragment
import com.weme.common.util.*
import com.weme.common.viewmodel.LookBigImgViewMode
import com.weme.mine.databinding.FragmentUserImgBinding
import com.weme.mine.viewmode.UserInfoViewMode


class UserInfoImgFragment : BaseFragment<FragmentUserImgBinding, LookBigImgViewMode>() {
    companion object {
        private const val ARG_TYPE = "arg_type"
        private const val ARG_URL = "arg_url"
        private const val IS_ME = "isMe"

        fun newInstance(type: Int, url: String, isMe: Boolean): UserInfoImgFragment {
            val fragment = UserInfoImgFragment()
            val args = Bundle()
            args.putInt(ARG_TYPE, type)
            args.putString(ARG_URL, url)
            args.putBoolean(IS_ME, isMe) // Use correct argument name
            fragment.arguments = args
            return fragment
        }
    }

    private val userInfoViewMode by lazy {
        ViewModelProvider(requireActivity()).get(UserInfoViewMode::class.java)
    }

    override fun startOperate() {
        val type = arguments?.getInt(ARG_TYPE)?:0
        val url = arguments?.getString(ARG_URL)
        val isMe = arguments?.getBoolean(IS_ME) ?: false



        var vipLevel = UserMKHelper.decodeLong(UserMKHelper.VIP_LEVEL)
        if(isMe){//如果查看自己默认有vip
            vipLevel = 1
        }
        mBinding.image.setOnClickListener {
           // if (vipLevel > 0){
                userInfoViewMode.clickBigImgLiveData.postValue("")
//            }else{
//                showToast("跳转订阅商城")
//            }

        }
        mBinding.imPlay.setVisible(!type.isTypeImg())
        //如果是图片 或者已订阅
        if ( type.isTypeImg() || vipLevel > 0) {
            Glide.with(mBinding.image)
                .load(url)
                .sizeMultiplier(0.5f)
                .override(requireActivity().deviceSize.width,requireActivity().deviceSize.height/2)
                .error(com.example.resources.R.mipmap.icon_mine_pic_null)
                .listener(object : RequestListener<Drawable> {
                    override fun onResourceReady(resource: Drawable, model: Any, target: Target<Drawable>?, dataSource: DataSource, isFirstResource: Boolean): Boolean {
                        mBinding.image.fadeIn()
                        return false
                    }

                    override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>, isFirstResource: Boolean): Boolean {
                        return false
                    }
                })
                .into(mBinding.image)

        } else {

            Glide.with(requireContext())
                .load(url)
                .sizeMultiplier(0.5f)
                .override(requireActivity().deviceSize.width, requireActivity().deviceSize.height / 2)
                .error(com.example.resources.R.mipmap.icon_mine_pic_null)
                .apply(RequestOptions
                    .placeholderOf(com.example.resources.R.drawable.icon_album_null)
                    .transform(GlideBlurTransformer(20, 2))
                )
                .listener(object : RequestListener<Drawable> {
                    override fun onResourceReady(resource: Drawable, model: Any, target: Target<Drawable>?, dataSource: DataSource, isFirstResource: Boolean): Boolean {
                        mBinding.image.fadeIn()
                        return false
                    }

                    override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>, isFirstResource: Boolean): Boolean {
                        return false
                    }
                })
                .into(mBinding.image)

        }

    }

    override fun onDestroy() {
        super.onDestroy()
        Glide.get(mBinding.image.context).clearMemory()
        Glide.get(requireContext()).clearMemory()
    }

    override fun clickListener() {
    }

    override fun getViewModel(): Class<LookBigImgViewMode> = LookBigImgViewMode::class.java
}
